<template>
  <div class="login">
    <img src="../assets/1.jpeg" class="logo" />
    <el-form :model="form" ref="form" :rules="rules" label-width="80px" status-icon>
      <el-form-item label="用户名" prop="username">
        <el-input v-model="form.username" placeholder="请输入用户名"  @keyup.enter.native="login"></el-input>
      </el-form-item>
      <el-form-item label="密码" prop="password">
        <el-input v-model="form.password" type="password" placeholder="请输入密码"  @keyup.enter.native="login"></el-input>
      </el-form-item>
      <el-form-item >
        <el-button type="primary" class="loginBtn" @click="login">登录</el-button>
        <el-button  @click="resetForm">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>
<script>
export default {
  data () {
    return {
      form: {
        username: '',
        password: ''
      },
      rules: {
        username: [
          { required: true, message: '请输入用户名', trigger: ['change', 'blur'] },
          { min: 3,
            max: 12,
            message: '用户名必须是3-12位',
            trigger: ['change', 'blur'] }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: ['change', 'blur'] },
          { min: 3,
            max: 8,
            message: '用户名必须是3-8位',
            trigger: ['change', 'blur'] }
        ]
      }
    }
  },
  methods: {
    resetForm () {
      // 调用form组件的重置方法
      this.$refs.form.resetFields()
    },
    login () {
      this.$refs.form.validate((isValid) => {
        if (!isValid) return
        // axios ( url ,data)
        this.$axios.post('login', this.form).then(res => {
          // 结构赋值
          // res.data.meta ={meta}=res.data
          console.log(res)
          const { meta, data } = res
          if (meta.status === 200) {
            // this.$message({
            //   showClose: true,
            //   type: 'success',
            //   message: meta.msg
            // })
            // 把token令牌存到本地
            localStorage.setItem('token', data.token)
            // 提示模态框
            this.$message.success(meta.msg)
            this.$router.push('/index')
          } else {
            this.$message.error(meta.msg)
          }
        })
      })
    }
  }
}

</script>
<style lang="scss">
.login {
  height: 100%;
  width: 100%;
  background-color: #2d434c;
  position: relative;
  .el-form {
    width: 400px;
    background-color: #fff;
    padding: 70px 20px 20px 20px;
    border-radius: 8px;
    position: absolute;
    top: 0;
    left: 50%;
    transform: translateX(-50%);
    margin-top: 220px;
    .loginBtn{
      margin-right: 80px;
    }
  }
  .logo {
    width: 110px;
    height: 110px;
    border-radius: 50%;
    border: 4px solid #ffff;
    position: absolute;
    top: 160px;
    left: 50%;
    transform: translateX(-50%);
    z-index: 999;
  }
}
</style>
